#!/usr/local/bin/php
<?php

/*
 * Copyright (C) 2014-2019 Franco Fichtner <franco@opnsense.org>
 * Copyright (C) 2004-2009 Scott Ullrich <sullrich@gmail.com>
 * Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>
 * Copyright (C) 2009 Erik Kristensen <erik@erikkristensen.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/* looks weird, but means we started PHP successfully */
echo "done.\n";

echo "Initializing...";
flush();

$inc_files = array(
    'config.inc',
    'console.inc',
    'auth.inc',
    'util.inc',
    'interfaces.inc',
    'system.inc',
    'filter.inc',
    'rrd.inc',
);

foreach ($inc_files as $inc_file) {
    require_once $inc_file;
    echo '.';
    flush();
}

echo "done.\n";

/*
 * If this was an import or restore or reset, once run
 * the migration, but do not display the status here as
 * otherwise this prints all the models that keep their
 * current version level.
 */
convert_config();

/*
 * Now mute console messages from kernel for this script, it
 * has consequences for printing bootup info and can clobber
 * stty probing during interface auto-detection.
 */
system_console_mute();

system_devd_configure(true);
system_login_configure(true);

/*
 *  Determine if we need to throw an interface exception
 *  and ask the user to reassign interfaces.  This will
 *  avoid a reboot and thats a good thing.
 */
if (is_interface_mismatch()) {
    echo "\nDefault interfaces not found -- Running interface assignment option.\n";
    while (!set_networking_interfaces_ports(true));
}

/* loopback device is required early for syslog */
interfaces_loopback_configure(true);

system_kernel_configure(true);
system_sysctl_configure(true);
system_timezone_configure(true);
system_firmware_configure(true);
system_trust_configure(true);
system_hostname_configure(true);
system_hosts_generate(true);
system_syslogd_start(true);

interfaces_configure(true);

system_resolvconf_generate(true);
filter_configure_sync(true);
plugins_configure('earlybootup', true);
system_cron_configure(true, true);
system_routing_configure(true);

system_hosts_generate(true);
plugins_configure('dhcp', true);
plugins_configure('dhcrelay', true);
plugins_configure('dns', true);

prefer_ipv4_or_ipv6();
plugins_configure('monitor', true);
filter_configure_sync(true);
plugins_configure('vpn', true);
plugins_configure('bootup', true);
rrd_configure(true);

/* XXX this seems misplaced */
system_powerd_configure(true);

/*
 * Give syslogd a kick after everything else has been
 * initialized, otherwise it can occasionally fail to
 * route syslog messages properly on both IPv4 and IPv6.
 */
system_syslogd_start(true, true);

system_console_unmute();

exit(0);
